<?php
add_action('admin_init','instal_database');
function instal_database()
{
    global $wpdb;
   $table_payment=$wpdb->prefix .'payments';
if ($wpdb->get_var("SHOW TABLES LIKE '$table_payment'") != $table_payment) {
		$wpdb->query("CREATE TABLE {$table_payment}(			
				  id  mediumint(10) AUTO_INCREMENT PRIMARY KEY ,
				  txnid varchar(20)  NULL,
				  sessionID VARCHAR(50) NOT NULL,
				  orderid  int(11) NOT NULL,
				  amount float(40)  NULL,
				  status varchar(25) NOT NULL,
				  currency varchar(10) NOT NULL,
				  payment_type  varchar(40) NOT NULL,
				  transaction_infor  longtext NULL,
				  createdtime int(11) NOT NULL
			
		)ENGINE=MyISAM DEFAULT CHARSET=utf8");
	}
}

function save_transaction_into_paypal($orderid,$amount,$currency,$payment_type,$transaction_infor,$status)
{
     global $wpdb;
     $payment_table=$wpdb->prefix. 'payments';
     $data=array(
      'id'=>0,
      'txnid'=>'',
      'sessionid'=>session_id(),
      'orderid'=>$orderid,
      'amount'=>$amount,
      'status'	=> $status,
      'currency'=>$currency,
      'payment_type' =>$payment_type,
      'transaction_infor'=>$transaction_infor,
      'createdtime'=>time()+7*3600
     ); 
    	  
    $run=$wpdb->insert($payment_table,$data);
    
    if($run)
    {
      return true;
    } 
    return FALSE;

}

function  update_transaction_status($orderid)
{	           
    global  $wpdb;
    $payment_table	 = $wpdb->prefix .'payments';
    $txnid 		     = $_POST['txn_id'];
	$payment_amount  = $_POST['mc_gross'];
	$payment_status  = $_POST['payment_status'];
	$item_number	 = $_POST['item_number'];
	$currency   	 = $_POST['mc_currency'];
	$payment_type	 = $_POST['payment_type'];
    $run=$wpdb->query("update {$payment_table} set txnid='{$txnid}',amount={$payment_amount},status='{$payment_status}',currency='{$currency}' where orderid ={$orderid}");
	if($run)
	{		           
		global  $wpdb;      
        $result=$wpdb->get_results("select ID from visa_applications where ID={$item_number}");
	    $user_id=$result[0]->ID;

		$user_data=get_userdata($user_id);
        $email=$user_data->user_email ;        
       		
	}
}


/*function updatePayments($txn_id,$payment_amount,$payment_status,$item_number,$currency,$payment_type)
{	
    global $wpdb;
    $data=array(
    'id'=>0,
    'txnid'=>$txn_id,
    'sessionid'=>session_id(),
    'orderid'=>$item_number,
    'amount'=>$payment_amount,
    'status'=>$payment_status,
    'currency'=>$currency,
    'payment_type'=>$payment_type,
    'createdtime'=>time()+7*3600
    );
    $table_payment=$wpdb->prefix .'payments';
   $run=$wpdb->insert($table_payment,$data);
	if($run)
	{	
    return  TRUE;
	}
	
	
	return FALSE;
    
}*/

function update_return_from_paypal()
{
	
  if (isset($_POST["txn_id"]) && isset($_POST["txn_type"])){
  
  	  if(isset($_POST['payer_status'])=='verified')
  	  {  	               	        
					$txnid 			 =$_POST['txn_id'];
					//$payment_amount  =$_POST['mc_gross'];
					//$payment_status  =$_POST['payment_status'];
					$item_number	 =$_POST['item_number'];
					//$currency   	 =$_POST['mc_currency'];
					// $payment_type	 =$_POST['payment_type'];
		if(check_txnid($txnid))		
		{
  	      update_transaction_status($item_number);  	      
  	       return TRUE;  	      
		}
		return false;
  	  }
   return FALSE;
  }


}